package com.zhcx.zhcxlibrary.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.zhcx.zhcxlibrary.R;
import com.zhcx.zhcxlibrary.utils.AnimationLoader;
import com.zhcx.zhcxlibrary.utils.DeviceUtils;


/**
 * ProjectName：Clientcar
 * PackageName：com.zhcx.wisdombus.widget
 * description:
 * Date：2016/8/6 16:27
 * Author：Steven
 * ClassName:ColorDialog
 **/
public class ChooseDialog extends Dialog implements View.OnClickListener {

    private Bitmap mContentBitmap;
    private View mBkgView, mDialogView;
    private LinearLayout llContent, linear_positive, linear_negative;
    private TextView mTitleTv, text_positive, text_negative, text_content;
    private Drawable mDrawable;
    private AnimationSet mAnimIn, mAnimOut;
    private int mResId, mBackgroundColor, mTitleTextColor, mContentTextColor;
    private OnPositiveListener mPositiveListener;
    private OnNegativeListener mNegativeListener;
    private CharSequence mTitleText, mContentText, mPositiveText, mNegativeText;
    private boolean mIsShowAnim;

    public ChooseDialog(Context context) {
        this(context, 0);
    }

    public ChooseDialog(Context context, int theme) {
        super(context, R.style.color_dialog);
        init();
    }

    private void callDismiss() {
        super.dismiss();
    }

    private void init() {
        mAnimIn = AnimationLoader.getInAnimation(getContext());
        mAnimOut = AnimationLoader.getOutAnimation(getContext());
        initAnimListener();
    }

    @Override
    public void setTitle(CharSequence title) {
        mTitleText = title;
    }

    @Override
    public void setTitle(int titleId) {
        setTitle(getContext().getText(titleId));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View contentView = View.inflate(getContext(), R.layout.layout_colordialog, null);
        setContentView(contentView);

        mDialogView = getWindow().getDecorView().findViewById(android.R.id.content);
        mBkgView = contentView.findViewById(R.id.llBkg);
        mTitleTv = (TextView) contentView.findViewById(R.id.tvTitle);
        llContent = (LinearLayout) contentView.findViewById(R.id.llContent);
        text_content = (TextView) contentView.findViewById(R.id.text_content);

        text_positive = (TextView) contentView.findViewById(R.id.text_positive);
        text_negative = (TextView) contentView.findViewById(R.id.text_negative);

        linear_positive = (LinearLayout) contentView.findViewById(R.id.linear_positive);
        linear_negative = (LinearLayout) contentView.findViewById(R.id.linear_negative);

        linear_positive.setOnClickListener(this);
        linear_negative.setOnClickListener(this);

        mTitleTv.setText(mTitleText);
        text_content.setText(mContentText);

        text_positive.setText(mPositiveText);
        text_negative.setText(mNegativeText);

        if (null == mPositiveListener && null == mNegativeListener) {
            llContent.setVisibility(View.GONE);
        } else if (null == mPositiveListener && null != mNegativeListener) {
            linear_positive.setVisibility(View.GONE);
            linear_negative.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.sel_def_gray));
        } else if (null != mPositiveListener && null == mNegativeListener) {
            linear_negative.setVisibility(View.GONE);
            linear_positive.setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.sel_def_gray));
        }
//        if (null != mDrawable) {
//            mContentIv.setBackgroundDrawable(mDrawable);
//        }
//        if (null != mContentBitmap) {
//            mContentIv.setImageBitmap(mContentBitmap);
//        }
//        if (0 != mResId) {
//            mContentIv.setBackgroundResource(mResId);
//        }
        setTextColor();
        setBackgroundColor();
//        setContentMode();
    }

    @Override
    protected void onStart() {
        super.onStart();
        startWithAnimation(mIsShowAnim);
    }

    @Override
    public void dismiss() {
        super.dismiss();
//        dismissWithAnimation(mIsShowAnim);
    }

    private void startWithAnimation(boolean showInAnimation) {
        if (showInAnimation) {
            mDialogView.startAnimation(mAnimIn);
        }
    }

    private void dismissWithAnimation(boolean showOutAnimation) {
        if (showOutAnimation) {
            mDialogView.startAnimation(mAnimOut);
        } else {
            super.dismiss();
        }
    }

    private void initAnimListener() {
        mAnimOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mDialogView.post(new Runnable() {
                    @Override
                    public void run() {
                        callDismiss();
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    private void setBackgroundColor() {
        if (0 == mBackgroundColor) {
            return;
        }
        int radius = DeviceUtils.dip2px(getContext(), 6);
        float[] outerRadii = new float[]{radius, radius, radius, radius, 0, 0, 0, 0};
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii, null, null);
        ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
        shapeDrawable.getPaint().setColor(mBackgroundColor);
        shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        mBkgView.setBackgroundDrawable(shapeDrawable);
    }

    private void setTextColor() {
        if (0 != mTitleTextColor) {
            mTitleTv.setTextColor(mTitleTextColor);
        }
//        if (0 != mContentTextColor) {
//            mContentTv.setTextColor(mContentTextColor);
//        }

    }

//    private void setContentMode() {
//        boolean isImageMode = (null != mDrawable | null != mContentBitmap | 0 != mResId);
//        boolean isTextMode = (!TextUtils.isEmpty(mContentText));
//
//        if (isImageMode && isTextMode) {
//            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mContentTv.getLayoutParams();
//            params.gravity = Gravity.BOTTOM;
//            mContentTv.setLayoutParams(params);
//            mContentTv.setBackgroundColor(Color.BLACK);
//            mContentTv.getBackground().setAlpha(0x28);
//            mContentTv.setVisibility(View.VISIBLE);
//            mContentIv.setVisibility(View.VISIBLE);
//            return;
//        }
//
//        if (isTextMode) {
//            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mContentTv.getLayoutParams();
//            params.gravity = Gravity.NO_GRAVITY;
//            mContentTv.setLayoutParams(params);
//            mContentIv.setVisibility(View.GONE);
//            mContentTv.setVisibility(View.VISIBLE);
//            return;
//        }
//
//        if (isImageMode) {
//            mContentTv.setVisibility(View.GONE);
//            mContentIv.setVisibility(View.VISIBLE);
//            return;
//        }
//
//    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.linear_positive == id) {
            LogUtils.d("linear_positive");
            mPositiveListener.onClick(this);
        } else if (R.id.linear_negative == id) {
            LogUtils.d("linear_negative");
            mNegativeListener.onClick(this);
        }
    }

    public ChooseDialog setAnimationEnable(boolean enable) {
        mIsShowAnim = enable;
        return this;
    }

    public ChooseDialog setAnimationIn(AnimationSet animIn) {
        mAnimIn = animIn;
        return this;
    }

    public ChooseDialog setAnimationOut(AnimationSet animOut) {
        mAnimOut = animOut;
        initAnimListener();
        return this;
    }

    public ChooseDialog setColor(int color) {
        mBackgroundColor = color;
        return this;
    }

    public ChooseDialog setColor(String color) {
        try {
            setColor(Color.parseColor(color));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return this;
    }

    public ChooseDialog setTitleTextColor(int color) {
        mTitleTextColor = color;
        return this;
    }

    public ChooseDialog setTitleTextColor(String color) {
        try {
            setTitleTextColor(Color.parseColor(color));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return this;
    }

    public ChooseDialog setContentTextColor(int color) {
        mContentTextColor = color;
        return this;
    }

    public ChooseDialog setContentTextColor(String color) {
        try {
            setContentTextColor(Color.parseColor(color));
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return this;
    }

    public ChooseDialog setPositiveListener(CharSequence text, OnPositiveListener l) {
        mPositiveText = text;
        mPositiveListener = l;
        return this;
    }

    public ChooseDialog setPositiveListener(int textId, OnPositiveListener l) {
        return setPositiveListener(getContext().getText(textId), l);
    }

    public ChooseDialog setNegativeListener(CharSequence text, OnNegativeListener l) {
        mNegativeText = text;
        mNegativeListener = l;
        return this;
    }

    public ChooseDialog setNegativeListener(int textId, OnNegativeListener l) {
        return setNegativeListener(getContext().getText(textId), l);
    }

    public ChooseDialog setContentText(CharSequence text) {
        mContentText = text;
        return this;
    }

    public ChooseDialog setContentText(int textId) {
        return setContentText(getContext().getText(textId));
    }

    public ChooseDialog setContentImage(Drawable drawable) {
        mDrawable = drawable;
        return this;
    }

    public ChooseDialog setContentImage(Bitmap bitmap) {
        mContentBitmap = bitmap;
        return this;
    }

    public ChooseDialog setContentImage(int resId) {
        mResId = resId;
        return this;
    }

    public CharSequence getContentText() {
        return mContentText;
    }

    public CharSequence getTitleText() {
        return mTitleText;
    }

    public CharSequence getPositiveText() {
        return mPositiveText;
    }

    public CharSequence getNegativeText() {
        return mNegativeText;
    }

    public interface OnPositiveListener {
        void onClick(ChooseDialog dialog);
    }

    public interface OnNegativeListener {
        void onClick(ChooseDialog dialog);
    }
}
